<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_mc_auth.class.php 2011-07-07 11:55:55 $
 * @Show Note: 设备验收批次
 */
Class zy_mc_auth extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_mc_auth()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_mc_auth.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_mc_auth");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign("main_menus",$this->get_user_menus());
		$this->smarty->assign('admin_info',get_admin_info());//取得管理员用户数组
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_mc_auth();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$is_jump = isset($_GET['is_jump']) ? intval($_GET['is_jump']) : 0;
		$is_show_sell = isset($_GET['is_show_sell']) ? intval($_GET['is_show_sell']) : 0;

		$col = "mc_auth_id";
		$where = 1; //全部

		if(isset($_REQUEST['post']) && trim($_REQUEST['post'])==1)
		{
			$mc_auth_no = isset($_POST['mc_auth_no']) ? trim($_POST['mc_auth_no']) : '';
			$contract_no = isset($_POST['contract_no']) ? trim($_POST['contract_no']) : '';
			$mc_auth_start_time = isset($_POST['mc_auth_start_time']) ? trim($_POST['mc_auth_start_time']) : '';
			$mc_auth_end_time = isset($_POST['mc_auth_end_time']) ? trim($_POST['mc_auth_end_time']) : '';
			
			if('' != $mc_auth_no) {
				$where .= " AND mc_auth_no LIKE '%{$mc_auth_no}%' ";
				$this->smarty->assign('mc_auth_no',$mc_auth_no);
			}
			if(!empty($contract_no)) {
				$where .= " AND contract_no LIKE '%{$contract_no}%' ";
				$this->smarty->assign('contract_no',$contract_no);
			}
			if(!empty($mc_auth_start_time)) {
				$where .= " AND mc_auth_start_time = '".strtotime($mc_auth_start_time)."' ";
				$this->smarty->assign('mc_auth_start_time',strtotime($mc_auth_start_time));
			}
			if(!empty($mc_auth_end_time)) {
				$where .= " AND mc_auth_end_time = '".strtotime($mc_auth_end_time)."' ";
				$this->smarty->assign('mc_auth_end_time',strtotime($mc_auth_end_time));
			}
		}
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);

		//对应验收记录
		$recordArr = array();
		$db_zaml = Factory::getDB('zy_auth_mc_log');
		foreach ($arr as $key=>$value) {
			$temp_arr =  $db_zaml->getRecord($value['mc_auth_id']);
			if(!empty($temp_arr)){
				$recordArr[$value['mc_auth_id']] = $temp_arr;
				foreach($temp_arr as $k => $v) {
					if($v['mc_log_id'] > 0) {
						$mcLogIdArr[] = $v['mc_log_id']; //执行ID
					}
				}
			}
		}
		//dump($mcLogIdArr);
		//getOne($mc_log_id);
		//dump($recordArr);
		$this->smarty->assign('recordArr',$recordArr);

		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		//$this->smarty->assign('act','list');
		if (empty($is_jump)) {
			$this->smarty->assign('act','list');			
		}else{
			$this->smarty->assign('is_show_sell',$is_show_sell);
			$this->smarty->assign('act','sel');
		}
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//合同号
			$contract_no = isset($_REQUEST['contract_no']) ?  trim($_REQUEST['contract_no']) : '';
			
			$this->smarty->assign('contract_no',$contract_no);
			//使用模版，展示提交界面
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();

			if(empty($temp['contract_no'])) {
				alertMsg2('请选择合同号');
			}
			if(empty($temp['mc_auth_no'])) {
				alertMsg2('验收报告编号不允许为空');
			}
			if(empty($temp['mc_auth_start_time'])) {
				alertMsg2('请选择开始验收日期');
			}
			if(empty($temp['mc_auth_end_time'])) {
				alertMsg2('请选择验收结束时间');
			}
			$record = $this->db->getRecord($temp['mc_auth_no']);
			if(!empty($record)) {
				alertMsg2('验收报告编号已存在');
			}

			$insrt = $this->db->insert_zy_mc_auth($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//alertMsg($error_no, 'index.php?c=zy_mc_auth');
			if(0 < $insrt) { //成功直接跳转
				redirectPage('index.php?c=zy_mc_auth');
			} else {
				alertMsg($error_no, 'index.php?c=zy_mc_auth');
			}
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$mc_auth_id = isset($_REQUEST['mc_auth_id']) ? intval(trim($_REQUEST['mc_auth_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($mc_auth_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_mc_auth');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "mc_auth_id=$mc_auth_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_mc_auth($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			//alertMsg($error_no, 'index.php?c=zy_mc_auth');
			if(0 <= $update) { //成功直接跳转
				redirectPage('index.php?c=zy_mc_auth');
			} else {
				alertMsg($error_no, 'index.php?c=zy_mc_auth');
			}
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$mc_auth_id = isset($_REQUEST['mc_auth_id']) ? intval(trim($_REQUEST['mc_auth_id'])) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($mc_auth_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_mc_auth');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}

		//对应验收记录
		$db_zaml = Factory::getDB('zy_auth_mc_log');
		//$recordArr =  $db_zaml->getJoinRecord($mc_auth_id);
		$recordArr =  $db_zaml->getDeviceInfo($mc_auth_id);
		//分页，通过查询所有来去
		$rowNo = count($recordArr);
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		//unset($zy_plan_reply_mc_arr);
		for ($i=$offset;$i<($numrows+$offset);$i++){
			if(isset($recordArr[$i]))
			$page_arr[] = $recordArr[$i];
		}
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		
		$this->smarty->assign('recordArr',$page_arr);
		
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$mc_auth_id = isset($_REQUEST['mc_auth_id']) ? intval(trim($_REQUEST['mc_auth_id'])) : 0 ;
		$where = "mc_auth_id=$mc_auth_id";
		$del=$this->db->delete_zy_mc_auth($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		//alertMsg($error_no, 'index.php?c=zy_mc_auth');
		if(0 < $del) { //成功直接跳转
			redirectPage('index.php?c=zy_mc_auth');
		} else {
			alertMsg($error_no, 'index.php?c=zy_mc_auth');
		}
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['mc_auth_id'] = $_POST['mc_auth_id'];
		} else {
			$temp['mc_auth_id'] = "";
		}
		//补充其他的字段
		$temp['mc_auth_no'] = $_POST['mc_auth_no'];
		$temp['mc_auth_start_time'] = strtotime($_POST['mc_auth_start_time']);
		$temp['mc_auth_end_time'] = strtotime($_POST['mc_auth_end_time']);
		$temp['mc_auth_state'] = $_POST['mc_auth_state'];
		$temp['mc_auth_info'] = $_POST['mc_auth_info'];
		$temp['contract_no'] = $_POST['contract_no'];
		$temp['admin_user_id'] = $_POST['admin_user_id'];
		$temp['file_path'] = $_POST['file_path'];
				
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'设备验收'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'updateFile' => array('updateFile','文件上传',32,false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}

	/**
	 * (功能描述) 文件上传
	 *
	 * @param 
	 * @return
	 */
	function updateFile() {
		$file_path = isset($_FILES["file_path"]) ? $_FILES["file_path"]: '';//附件名称
		$mc_auth_id = isset($_REQUEST['mc_auth_id']) ? intval(trim($_REQUEST['mc_auth_id'])) : 0 ;
		$error_no = 0;
		//判断是否存在文件上传
		if (!empty($file_path['name'])) {
			$array = array(
				'source_file_name'=>$file_path['name'],
			);
			$file_name = upload_file($_FILES,'file_path',md5($file_path['name'].time()),$_GET['c']);
			$array['file_path'] = $file_name;
			$where = "mc_auth_id = '{$mc_auth_id}' ";
			$update = $this->db->update_zy_mc_auth($array,$where);
			//print_r($update);
			$error_no = 0 > $update ?  $update :'-1502';
		}
		if ($error_no == '-1502') {
			echo json_encode(array('code'=>1));exit;
		}else{
			echo json_encode(array('code'=>2));exit;
		}
	}

}

?>